const mysql = require('mysql2/promise');

async function checkDatabase() {
    // 数据库配置
    const dbConfig = {
        host: '182.160.6.140',
        user: 'root',
        password: 'BinanceRoot2023',
        database: 'binance_klines'
    };

    try {
        // 创建连接
        const connection = await mysql.createConnection(dbConfig);
        
        // 检查所有K线表的数据量和时间范围
        const intervals = ['5m', '10m', '15m', '30m', '1h', '1d'];
        const symbols = ['BTCUSDT', 'ETHUSDT'];
        
        for (const interval of intervals) {
            console.log(`\n检查 ${interval} 周期数据:`);
            console.log('-'.repeat(50));
            
            for (const symbol of symbols) {
                const [rows] = await connection.execute(
                    `SELECT 
                        COUNT(*) as count,
                        MIN(open_time) as earliest_time,
                        MAX(open_time) as latest_time,
                        COUNT(DISTINCT DATE(FROM_UNIXTIME(open_time/1000))) as days_count
                    FROM kline_${interval}
                    WHERE symbol = ?`,
                    [symbol]
                );
                
                const data = rows[0];
                if (data.count > 0) {
                    console.log(`\n${symbol}:`);
                    console.log(`总数据量: ${data.count.toLocaleString()} 条`);
                    console.log(`最早数据: ${new Date(data.earliest_time).toLocaleString()}`);
                    console.log(`最新数据: ${new Date(data.latest_time).toLocaleString()}`);
                    console.log(`覆盖天数: ${data.days_count} 天`);
                    
                    // 计算数据完整性
                    const timeSpan = data.latest_time - data.earliest_time;
                    const intervalMinutes = {
                        '5m': 5,
                        '10m': 10,
                        '15m': 15,
                        '30m': 30,
                        '1h': 60,
                        '1d': 1440
                    }[interval];
                    
                    const expectedCount = Math.floor(timeSpan / (intervalMinutes * 60 * 1000));
                    const completeness = (data.count / expectedCount * 100).toFixed(2);
                    console.log(`数据完整性: ${completeness}%`);
                    
                    // 检查是否需要重新同步
                    if (completeness < 95) {
                        console.log(`⚠️ 建议重新同步 ${symbol} ${interval} 数据`);
                    }
                } else {
                    console.log(`\n${symbol}: ⚠️ 无数据，需要同步`);
                }
            }
        }
        
        // 关闭连接
        await connection.end();
        
    } catch (error) {
        console.error('查询数据库失败:', error);
    }
}

// 执行检查
checkDatabase(); 